<?php
class Course_controller extends CI_Controller
{
    //Ham dung
    public function construct()
    {
        parent::__construct;
        $this->load->helper("url");
        //$this->load->model('course_model');
    }
    
    public function show_fedx_course_overview($course_id) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            $this->load->model('course_model');
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            $add_data['course_id'] = $course_id;
            
            $courses = $this->cache->apc->get('user_reg_courses', $add_data['user']['ID']);
            //var_dump($courses);
            $i = 0;
            while (($courses[$i]['course_id'] != $course_id) && ($i <= 1000)) {
                $i++;
            } 
            //var_dump($courses[$i]);
            $add_data['current_course'] = $courses[$i];           
            $add_data['current_course']['long_introduce'] = $this->course_model->get_long_introduce_of_course($course_id);
            $this->cache->apc->save('current_course',$add_data['user']['ID'], $add_data['current_course'], 30000);
            $add_data['lectures'] = $this->course_model->get_all_lectures($course_id);
            $this->cache->apc->save('current_lectures',$add_data['user']['ID'], $add_data['lectures'], 30000);
            $this->load->view('course_view/course_overview/course_fedx_overview', $add_data);
        } else {
            $this->load->view('');
        }
        
    }
    
    
    //Ham xem noi dung mon hoc ma khong can dang nhap
    //Duoc goi den khi nhan vao thanh thong bao ngay khai giang va ten giao vien tren trang chu
    //Hoac xem gioi thieu o trang nguoi dung, khi chua dang nhap khoa hoc
    public function course_overview_unsign ($course_id) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        
        $this->load->model('course_model');
        $add_data['current_course'] = $this->course_model->get_one_course($course_id);
        $add_data['current_course']['long_introduce'] = $this->course_model->get_long_introduce_of_course($course_id);
        $this->load->view('course_view/course_overview/course_overview_unsign', $add_data);
        
    } 
    
    public function show_lecture_ego($lecture_id) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));          
            
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']);
            $add_data['course_id'] = $add_data['current_course']['course_id'];
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
            $this->cache->apc->save('current_lecture_id', $add_data['user']['ID'], $lecture_id, 30000);
            $add_data['pdf_path'] = $add_data['lectures'][$lecture_id-1]['path'];
            $this->load->view('course_view/pdfs/display_lecture_pdf_ego', $add_data);
        } else {
            $this->load->view('');
        }
    }
    
    public function show_excercise ($lecture_id) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));          
            
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']);
            $add_data['course_id'] = $add_data['current_course']['course_id'];
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures', $add_data['user']['ID']);
            $this->cache->apc->save('current_lecture_id', $add_data['user']['ID'], $lecture_id, 30000);
            $this->load->model('course_model');
            $add_data['excercise_pdf'] = $this->course_model->get_some_excercise($add_data['course_id'], $lecture_id);
            //$add_data['pdf_path'] = $add_data['excercise_pdf']['path']; 
            $this->load->view('course_view/pdfs/display_excercise_pdf_ego', $add_data);
        } else {
            $this->load->view('');
        }
    }
    
    public function show_all_video($lecture_id){
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
                       
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']); 
            
            $this->load->model('course_model');
            $add_data['video'] = $this->course_model->get_all_video($add_data['current_course']['course_id'], $lecture_id);
            //echo $add_data['video'][1][1]['path'];
            
            $this->cache->apc->save('current_course_video_frament', $add_data['user']['ID'], $add_data['video'],30000);
            $this->cache->apc->save('current_lecture_id', $add_data['user']['ID'], $lecture_id, 30000);
            $add_data['current_lecture_id'] = $lecture_id;
            
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures',$add_data['user']['ID']);
            
            $this->load->view('course_view/videos/show_video', $add_data);
        } else {
        
        }
    }
    
    public function display_video($path_index) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
                        
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            $data = $this->cache->apc->get('current_course_video_frament', $add_data['user']['ID']);         
          
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']);
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures',$add_data['user']['ID']);
            
            $add_data['video']['path'] = $data[$path_index]['path'];   
                          
            $this->load->view('course_view/videos/display_video', $add_data);
        } else {
        
        }
    }
    
    
    public function show_course_overview ($name) { //Post cac bai giang tren dai hoc
        $this->load->helper('url');
        if ($name == 'dm2013') {          
            
            $this->load->view('course_view/course_overview/course_overview');
        }
        else if ($name == 'int10052013'){
            $this->load->view('course_view/course_overview/course_int10052013_overview');
        }
        else if ($name == 'thcs4_nmh') {
            $course['dir'] = "public/thcs4_nmh/examples/";  
            //echo $course['dir'];           
            if (is_dir($course['dir'])) {
                //echo "he he";
                $i = 0;
                if ($dh = opendir($course['dir'])) {
                    while (($file = readdir($dh)) !== false) {
                        $course['file_name'][$i] = $file;
                        $course['file_extension'][$i] = filetype($course['dir'] . $file);
                        $i++;
                    }
                }
                $this->load->view('course_view/course_overview/course_thcs4_nmh', $course);
            }
            //$this->load->view('course_view/course_overview/course_thcs4_nmh', $course);
        }
    }
    
    public function display_pdf ($file_name) {
        $this->load->helper('url');
        $add_data['path'] = base_url()."public/lecture_slides/discrete_mathematical_models/".$file_name;
        $this->load->view('course_view/pdfs/display_pdf',$add_data);
    }
    
    public function display_pdf_int10052013 ($file_name) {
        $this->load->helper('url');
        $add_data['path'] = base_url()."public/lecture_slides/int10052013/".$file_name;
        $this->load->view('course_view/pdfs/display_pdf',$add_data);
    }
    
    //Trang giao diện nộp bài
    public function upload_excercise ($lecture_id) {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
                        
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));        
          
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']);
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures',$add_data['user']['ID']);
            
            $this->cache->apc->save('current_lecture_id', $add_data['user']['ID'], $lecture_id, 30000);
            $this->load->model('course_model');
               
            $add_data['current_lecture'] = $this->course_model->get_one_lecture($lecture_id);
                          
            $this->load->view('course_view/upload_file/upload_excercise', $add_data);
        } else {
        
        }        
        
    }
    
    public function save_file() {
        $this->load->library('session');
        $add_data['user'] = $this->session->all_userdata();
        if($this->session->userdata('validated') != null) {
            $this->load->helper('url');
            
            $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
            
            $lecture_id = $this->cache->apc->get('current_lecture_id', $add_data['user']['ID']);
            //echo $lecture_id;
            $add_data['current_course'] = $this->cache->apc->get('current_course',$add_data['user']['ID']);
            $add_data['course_id'] = $add_data['current_course']['course_id'];
            $add_data['lectures'] =  $this->cache->apc->get('current_lectures',$add_data['user']['ID']);
            
            $save_directory = "upload_file/excercise/" . $add_data['current_course']['course_code']."/" . $add_data['user']['EMAIL']."/";
            
            $allowedExts = array("gif", "jpeg", "jpg", "png", "zip", "rar", "pdf", "doc", "docx", ".deb");
            $temp = explode(".", $_FILES["file"]["name"]);
            $extension = end($temp);
            
            //Tuong ung giua duoi file ho tro va dang khai bao trong php
            //.deb application/octet-stream
            
            if ((($_FILES["file"]["type"] == "image/gif")
                || ($_FILES["file"]["type"] == "image/jpeg")
                || ($_FILES["file"]["type"] == "image/jpg")
                || ($_FILES["file"]["type"] == "image/pjpeg")
                || ($_FILES["file"]["type"] == "image/x-png")
                || ($_FILES["file"]["type"] == "image/png")
                || ($_FILES["file"]["type"] == "application/x-rar-compressed")
                || ($_FILES["file"]["type"] == "application/x-zip-compressed")
                || ($_FILES["file"]["type"] == "application/pdf")
                || ($_FILES["file"]["type"] == "application/msword")
                || ($_FILES["file"]["type"] == "application/vnd.openxmlformats-officedocument.wordprocessingml.document")
                || ($_FILES["file"]["type"] == "application/octet-stream"))                  
                && ($_FILES["file"]["size"] < 7340033)
                || ($_FILES["file"]["type"] == "image/png"))

            {
                if ($_FILES["file"]["error"] > 0)
                {
                    $add_data['message']['error'] = "Return Code: " . $_FILES["file"]["error"] . "<br>";
                }
                else
                {

                    if (file_exists($save_directory . $_FILES["file"]["name"]))
                    {
                        $add_data['message']['success'] = "Tệp " . $_FILES["file"]["name"] . " đã tồn tại";
                    }
                    else
                    {
                       
                        
                        if (!file_exists($save_directory)) {
                            mkdir($save_directory, 0777, true);
                        }
                        
                        move_uploaded_file($_FILES["file"]["tmp_name"],
                        $save_directory . $_FILES["file"]["name"]);
                        $add_data['message']['success'] = "Đã lưu thành công tệp /" . $add_data['current_course']['course_code']."/" . $add_data['user']['EMAIL']."/" . $_FILES["file"]["name"];
                        
                        
                        
                        //Send email to excercise incharger
                        $this->load->model('user_model');
                        $to      = $this->user_model->get_in_charge_excercise_email($add_data['user']['ID'], $add_data['current_course']['course_code']);
                        $subject = 'Excercise of course '.$add_data['current_course']['course_name'].' from '.$add_data['user']['USERNAME']."\n".
                        $message =  'Student: '. $add_data['user']['USERNAME']."\n".
                                    'Email: '. $add_data['user']['EMAIL']."\n".
                                    //'Tuần thứ '.$lecture_id."\n".
                                    'Excercise\'s link: '.base_url().$save_directory . $_FILES["file"]["name"]."\n";
                        $headers = 'From: ego@edugreen.vn' . "\r\n" .
                                    'Reply-To: ego@edugreen.vn' . "\r\n" .
                                    'X-Mailer: PHP/' . phpversion();

                        $sent_mail = @mail($to, $subject, $message, $headers);
                        if ($sent_mail == FALSE) {
                            $this->user_model->insert_fail_mail_excercise($add_data['current_course']['course_code'],
                                                                            $add_data['user']['EMAIL'], 
                                                                            base_url().$save_directory . $_FILES["file"]["name"], 
                                                                            $to);
                        }
                        
                        //Gui mac dinh cho thay Vinh
                        $to      = "leanhvinh@gmail.com";
                        $subject = 'Excercise of course '.$add_data['current_course']['course_name'].' from '.$add_data['user']['USERNAME']."\n".
                        $message =  'Student: '. $add_data['user']['USERNAME']."\n".
                                    'Email: '. $add_data['user']['EMAIL']."\n".
                                    //'Tuần thứ '.$lecture_id."\n".
                                    'Excercise\'s link: '.base_url().$save_directory . $_FILES["file"]["name"]."\n";
                        $headers = 'From: ego@edugreen.vn' . "\r\n" .
                                    'Reply-To: ego@edugreen.vn' . "\r\n" .
                                    'X-Mailer: PHP/' . phpversion();

                        mail($to, $subject, $message, $headers);
                        
                        
                    }
                }
                
            }
            else
            {
                $add_data['message']['error'] = "Không hỗ trợ tải lên tệp định dạng " .$_FILES["file"]["type"] . "";
            }  
            //Return back to the course view page          
            
            $this->load->view('course_view/course_overview/after_upload_file', $add_data);       
        } 
        else 
        {
        
        }  
    }
    
    function update_all_grade() {
        $this->load->model('user_model');
        $this->user_model->update_all_grade();
    }
    
    
}
?>